# 浮点数的例子
pi = 3.14159
temperature = -10.5
salary = 5000.00
scientific = 6.02e23  # 科学计数法：6.02 × 10²³

print(f"scientific={scientific},pi={pi:.2f},temperature={temperature:.2f}")


# 从整数转换
float_from_int = float(42)  # 42.0

# 从字符串转换
float_from_str = float("3.14")  # 3.14
float_from_scientific = float("2.5e3")  # 2500.0

# 从布尔值转换
float_from_bool = float(True)  # 1.0
float_from_false = float(False)  # 0.0


import math

positive_infinity = float("inf")
print(f"positive_infinity={positive_infinity}\n")
print(f"positive_infinity is infinity={math.isinf(positive_infinity)}")


number = 3.9415926535

print(round(number))  # 3
print(round(number, 2))  # 3.14
print(round(number, 4))  # 3.1416

# 注意银行家舍入法（四舍六入五成双）
print(round(2.5))  # 2
print(round(3.5))  # 4
print(round(3.8))  # 4


test_x = 123456789.19912
print(f"保留2位小数千分位输出数字={test_x:,.2f}")

print(f"int类型数转换为浮点数={float(10)}\n")
print(f"负数转换为浮点数={float(-10)}\n")
print(f"bool类型的数转换为浮点数float(True)={float(True)}\n")
print(f"字符串类型的数字转换成浮点数float('100')={float('100')}")
